.SUFFIX: .c .cpp .s .o .d .h .inc

ifeq "$(OS)" "Windows_NT"
PATH_CONVERT = $(shell cygpath -m $(1))
else
PATH_CONVERT = $(1)
endif

### ARM core (used by option)
ARMCORE  := cortex-m0

TCHAIN          := $(GNUarm)

STARTUP_DEFS := -D__START=main
STARTUP_DEFS += -D__STARTUP_CLEAR_BSS
STARTUP_DEFS += -D__STARTUP_SET_SP
STARTUP_DEFS += -D__STARTUP_INT_VECT
STARTUP_DEFS += -D__STARTUP_HW

### Used by the option of armcc
CINCPATH := -I $(ROOT_DIR)/CMSIS/inc \
            -I $(ROOT_DIR)/FreeRTOS/inc \
            -I $(ROOT_DIR)/MILBCM0_API/MILBCM0_Header/include \
            -I $(ROOT_DIR)/MILBCM0_API/MILBCM0_Header/include/Arm \
            -I $(ROOT_DIR)/MILBCM0_API/MILBCM0_Header/include/Top \
            -I $(ROOT_DIR)/MILBCM0_API/MILBCM0_Header/include/Exs \
            -I $(ROOT_DIR)/MILBCM0_API/MILBCM0_Header/include/Pmc \
            -I $(ROOT_DIR)/MILBCM0_API/include \
            -I $(ROOT_DIR)/MILBCM0_API/Project/ComponentTest/include \
            -I $(ROOT_DIR)/Base/init/inc \
            -I $(ROOT_DIR)/Base/statemachine/inc \
            -I $(ROOT_DIR)/Base/CMDinterpreter/inc \
            -I $(ROOT_DIR)/Base/sequencer/inc \
            -I $(ROOT_DIR)/Base/adc/inc \
            -I $(ROOT_DIR)/Base/i2c/inc \
            -I $(ROOT_DIR)/Base/spi/inc \
            -I $(ROOT_DIR)/Base/TemperatureSensor/inc \
            -I $(ROOT_DIR)/Base/uart/inc \
            -I $(ROOT_DIR)/Base/nfc/inc \
            -I $(ROOT_DIR)/Base/emmc/inc \
            -I $(ROOT_DIR)/Base/loader/inc \
            -I $(ROOT_DIR)/Base/ipcu/inc \
            -I $(ROOT_DIR)/Base/calendar/inc \
            -I $(ROOT_DIR)/Custom/inc \

### DELETE_SECURE
CINCPATH += -I $(ROOT_DIR)/Base/crypto/inc \
### DELETE_SECURE

### Used by the option of armasm
AINCPATH := -I $(ROOT_DIR)/Base/init/inc \
            -I $(ROOT_DIR)/Custom/inc \

### Compile Option File
include $(ROOT_DIR)/Project/MILB_COMMON.opt

### Compiler
CC  := $(TCHAIN)gcc
CFLAGS  := -c
CFLAGS  += -pipe
CFLAGS  += -mthumb
CFLAGS  += -mcpu=$(ARMCORE)
CFLAGS  += -std=gnu90
CFLAGS  += -Os
CFLAGS  += -ffunction-sections
CFLAGS  += -fdata-sections
CFLAGS  += -fno-builtin
CFLAGS  += -g3
CFLAGS  += -gdwarf-2
CFLAGS  += -Wall
CFLAGS  += -D__thumb__
CFLAGS  += $(MILB_COMMON_OPT)

CFLAGS_DEPENDS  := -c
CFLAGS_DEPENDS  += -pipe
CFLAGS_DEPENDS  += -mthumb
CFLAGS_DEPENDS  += -mcpu=$(ARMCORE)
CFLAGS_DEPENDS  += -fsyntax-only
CFLAGS_DEPENDS  += -D__thumb__
CFLAGS_DEPENDS  += $(MILB_COMMON_OPT)

### Assember
AS  := $(TCHAIN)as
ASFLAGS  := -c
ASFLAGS  += -pipe
ASFLAGS  += $(STARTUP_DEFS)
ASFLAGS  += -mthumb
ASFLAGS  += -mcpu=$(ARMCORE)
ASFLAGS  += -g3
ASFLAGS  += -gdwarf-2
ASFLAGS  += -x assembler-with-cpp
ASFLAGS  += -Wa,--keep-locals
ASFLAGS  += -D__thumb__
ASFLAGS  += $(BOARD_OPT)
ASFLAGS  += $(SENSOR_OPT)
ASFLAGS  += $(MEM_SIZE_OPT)
ASFLAGS  += $(MILB_COMMON_OPT)

ASFLAGS_DEPENDS  := -c
ASFLAGS_DEPENDS  += -pipe
ASFLAGS_DEPENDS  += $(STARTUP_DEFS)
ASFLAGS_DEPENDS  += -mthumb
ASFLAGS_DEPENDS  += -mcpu=$(ARMCORE)
ASFLAGS_DEPENDS  += -x assembler-with-cpp
ASFLAGS_DEPENDS  += -fsyntax-only
ASFLAGS_DEPENDS  += -D__thumb__
ASFLAGS_DEPENDS  += $(BOARD_OPT)
ASFLAGS_DEPENDS  += $(SENSOR_OPT)
ASFLAGS_DEPENDS  += $(MEM_SIZE_OPT)
ASFLAGS_DEPENDS  += $(MILB_COMMON_OPT)

##### DELETE_RELEASE #####
### PGRelief
PGRFLAGS          = -c
PGRFLAGS         += --csv
PGRFLAGS         += --output_code SJIS
PGRFLAGS         += --slashcomment
PGRFLAGS         += --C99
PGRFLAGS         += $(CINCPATH)
PGRFLAGS         += -K GNU/GCC3_C90
PGRFLAGS         += -D__GNUC__
# PGRFLAGS         += -O "$(PGR_OPT_FILE)"
PGRFLAGS         += -S UTF8
PGRFLAGS         += -Y "$(gnu_root_path)\arm-none-eabi\include"
PGRFLAGS         += -Y "$(gnu_root_path)\lib\gcc\arm-none-eabi\4.9.3\include"
PGRFLAGS         += -Z "$(EPOMDIR)\MessageInfo\default-pgr.rul"
# Workaround of following PG-Relief error.
#   "C:\cygwin\opt\arm-none-eabi\arm-none-eabi\include\machine\ieeefp.h",406,"!","pgr0455","#error  Endianess not declared!!"
PGRFLAGS         += -D__IEEE_LITTLE_ENDIAN
##### DELETE_RELEASE #####


### The file and folder
SRC_DIR_NAME     := src
OBJ_DIR_NAME     := obj
DEP_DIR_NAME     := dep
# INC_DIR_NAME   := include
OBJ_FILE          = $(OBJ_DIR_NAME)/%.o
SRC_CFILE         = $(SRC_DIR_NAME)/%.c
SRC_AFILE         = $(SRC_DIR_NAME)/%.s
DEP_FILE          = $(DEP_DIR_NAME)/%.d
CUROBJSFILE       = $(ROOT_DIR)/$(WS_DIR_NAME)/$(OBJ_DIR_NAME)/$(strip $(subst /,_, $(DIR))).objs
##### DELETE_RELEASE #####
# PGR_OPT_FILE      = $(ROOT_DIR)\project\boot_strap.pgr
PGR_CSV_FILE      = $(ROOT_DIR)/PGRelief_out.csv
##### DELETE_RELEASE #####
CSRC_TMP          = $(patsubst %.c,$(SRC_DIR_NAME)/%.c,$(CSRC))
ASRC_TMP          = $(patsubst %.s,$(SRC_DIR_NAME)/%.s,$(ASRC))
OBJS              = $(CSRC_TMP:$(SRC_DIR_NAME)/%.c=$(OBJ_DIR_NAME)/%.o)
OBJS             += $(ASRC_TMP:$(SRC_DIR_NAME)/%.s=$(OBJ_DIR_NAME)/%.o)
DEPS              = $(CSRC_TMP:$(SRC_DIR_NAME)/%.c=$(DEP_DIR_NAME)/%.d)
DEPS             += $(ASRC_TMP:$(SRC_DIR_NAME)/%.s=$(DEP_DIR_NAME)/%.d)
ALL_SRC           = $(CSRC)
ALL_SRC          += $(ASRC)


QUIET := @

.PHONY: default target message clean clean-obj depends recursive-target $(SUBDIRS) \
        message-self target-self target-objs-self depends-self clean-depends-self clean-obj-self clean-self

default : target

message :
	$(QUIET)$(MAKE) recursive-target PRE_TARGET=message-self

target :
	$(QUIET)$(MAKE) recursive-target POST_TARGET=target-self IS_INCLUDE_DEPS=y

clean :
	$(QUIET)$(MAKE) recursive-target POST_TARGET=clean-self

clean-obj :
	$(QUIET)$(MAKE) recursive-target POST_TARGET=clean-obj-self

depends :
	$(QUIET)$(MAKE) recursive-target POST_TARGET=depends-self

recursive-target : $(PRE_TARGET) $(SUBDIRS) $(POST_TARGET)

ifneq "$(SUBDIRS)" ""
$(SUBDIRS) :
	$(QUIET)$(MAKE) recursive-target -C $@
endif


message-self :
ifneq "$(MESSAGE)" ""
	$(QUIET)echo $(MESSAGE)
endif


target-self : target-objs-self $(OBJS)

target-objs-self :
ifneq "$(words $(ALL_SRC))" "0"
	$(QUIET)if [ ! -d $(ROOT_DIR)/$(WS_DIR_NAME)/$(OBJ_DIR_NAME) ]; then mkdir $(ROOT_DIR)/$(WS_DIR_NAME)/$(OBJ_DIR_NAME); fi;
	$(QUIET)if [ -f $(CUROBJSFILE) ]; then rm -f $(CUROBJSFILE); fi;
	$(QUIET)echo $(patsubst $(OBJ_DIR_NAME)/%.o, $(DIR)/$(OBJ_DIR_NAME)/%.o, $(OBJS)) > $(CUROBJSFILE)
endif

depends-self : clean-depends-self $(DEPS)

clean-depends-self :
	$(QUIET)if [ $(DEP_DIR_NAME) ]; then rm -rf $(DEP_DIR_NAME); fi;

clean-obj-self :
	$(QUIET)if [ $(OBJ_DIR_NAME) ]; then rm -rf $(OBJ_DIR_NAME); fi;

clean-self : clean-obj-self clean-depends-self

distclean-self : clean-self


ifeq "$(IS_INCLUDE_DEPS)" "y"
-include $(DEPS)
endif

$(OBJ_FILE) : $(SRC_CFILE)
	$(QUIET)if [ ! -d $(OBJ_DIR_NAME) ]; then mkdir $(OBJ_DIR_NAME); fi;
##### DELETE_RELEASE #####
ifeq "$(IS_USE_PGRELIEF)" "y"
	$(QUIET)if [ ! -d $(TMP)\PGR_output]; then mkdir $(TMP)\PGR_output; fi;
	$(QUIET)"$(EPOMDIR)\pgr5.exe" $(PGRFLAGS) --temp-file "$(TMP)\PGR_output\$(<F)" "$(CURDIR)/$<" >> $(PGR_CSV_FILE)
endif
##### DELETE_RELEASE #####
	$(QUIET)echo compiling $< ...
	$(QUIET)$(CC) $(CFLAGS) $(CINCPATH) -o $@ $(call PATH_CONVERT, $(CURDIR)/$<)

$(OBJ_FILE) : $(SRC_AFILE)
	$(QUIET)if [ ! -f $(OBJ_DIR_NAME) ]; then mkdir -p $(OBJ_DIR_NAME); fi;
	$(QUIET)echo assembling $< ...
	$(QUIET)$(CC) $(ASFLAGS) $(AINCPATH) -o $@ $(call PATH_CONVERT, $(CURDIR)/$<)

$(DEP_FILE) : $(SRC_CFILE)
	$(QUIET)if [ ! -d $(DEP_DIR_NAME) ]; then mkdir -p $(DEP_DIR_NAME); fi;
	$(QUIET)echo creating $@ ...
	$(QUIET)$(CC) $(CFLAGS_DEPENDS) $(CINCPATH) -MT $(patsubst $(SRC_DIR_NAME)/%.c,$(OBJ_DIR_NAME)/%.o,$<) -MT $@ -M -MF $@ $< 	

$(DEP_FILE) : $(SRC_AFILE)
	$(QUIET)if [ ! -d $(DEP_DIR_NAME) ]; then mkdir -p $(DEP_DIR_NAME); fi;
	$(QUIET)echo creating $@ ...
	$(QUIET)$(CC) $(ASFLAGS_DEPENDS) $(AINCPATH) -MT $(patsubst $(SRC_DIR_NAME)/%.s,$(OBJ_DIR_NAME)/%.o,$<) -MT $@ -M -MF $@ $< 	
